<template>
	<view class="cont">
		<!-- <img class="top" src="../../../static/aidex/images/top.png" /> -->
		<img src="../../../static/aidex/images/favicon.png" class="logo"/>
		<img src="../../../static/aidex/images/slg.png" class="year" />
		<view class="from">
			<view class="item">
				<view class="name">
					头像
				</view>
				<view class="info">
					<image :src="vuex_user && vuex_user.headimgurl" mode=""></image>
				</view>
			</view>
			<view class="item">
				<view class="name">
					昵称
				</view>
				<view class="info">
					{{vuex_user && vuex_user.nickname}}
					<!-- <u-input v-model="model.name" type="text" /> -->
				</view>
			</view>

			<view class="item">
				<view class="name">
					手机号
				</view>
				<view class="info">
					<u-input placeholder="请输入手机号" v-model="model.phone" type="number" />
				</view>
			</view>


			<view class="item">
				<view class="name">
					验证码
				</view>
				<view class="info">
					<u-input maxlength='6' placeholder="请输入验证码" v-model="model.yzm" type="number" />
					<u-button class="btn" type="primary" size="mini" @click="getCode">{{codeTips}}</u-button>
				</view>
			</view>

			<u-button @click="register" class="sub" type="primary" shape="circle" :ripple="true"
				ripple-bg-color="#909399">立即注册</u-button>

			<u-verification-code endText="获取验证码" startText="获取验证码" seconds="60" ref="uCode" @change="codeChange">
			</u-verification-code>
		</view>
		<!-- <view class="footer">
			<image class="img" src="../../../static/aidex/images/footer.png" mode=""></image>
		</view> -->
	</view>
</template>
<script>
	export default {
		data() {
			return {

				codeTips: '',
				model: {
					head: '',
					name: '',
					phone: '',
					yzm: ""

				}
			}
		},
		components: {

		},
		onShow() {
			console.log(this.vuex_user, 78889);
		},
		methods: {
			codeChange(text) {
				this.codeTips = text;
			},

			register() {

				if (!this.model.phone) {
					this.$u.toast('手机号不能为空!');
					return
				}
				if(!this.$u.test.mobile(this.model.phone)){
					this.$u.toast('手机号格式不正确!');
					return
				}
				if (!this.model.yzm) {
					this.$u.toast('验证码不能为空');
					return
				}
				
				
				
				this.$u.api.register({
						"nickname": this.vuex_user.nickname, //昵称
						"headimgurl": this.vuex_user.headimgurl, //头像
						"openid": this.vuex_user.openid, //openid
						"tell": this.model.phone, //电话
						"smsCode": this.model.yzm //验证码
					})
					.then((res) => {

						if (res.code == 0) {
							this.$u.toast('注册成功');
							
							setTimeout(()=>{
								this.$u.vuex("vuex_user", res?.nkMember);
								this.$u.vuex("vuex_token2", res?.token);
								uni.switchTab({
									url:"/pages/sys/workbench/index"
								})
							},1500)
							
							

						} else {
							this.$u.toast(res.msg); // 通知验证码组件内部开始倒计时
							console.log("请求数据失败");
						}
					});

			},


			getCode() {


				if (this.$refs.uCode.canGetCode) {

					if (!this.model.phone) {
						this.$u.toast('手机号不能为空');
						return
					}
					
					if(!this.$u.test.mobile(this.model.phone)){
						this.$u.toast('手机号格式不正确!');
						return
					}

					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})



					this.$u.api.sendCode({
							tell: this.model.phone
						})
						.then((res) => {

							if (res.code == 0) {
								uni.hideLoading(); // 这里此提示会被this.start()方法中的提示覆盖
								this.$u.toast('验证码已发送'); // 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();

							} else {
								this.$u.toast(res.msg); // 通知验证码组件内部开始倒计时
								console.log("请求数据失败");
							}
						});



				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},


		}
	}
</script>
<style>
	page {
		background: #005850;
	}
</style>
<style lang="scss" scoped>
	.cont {
		.top {
			width: 100%;
			display: block;
			margin: 102rpx 0 0;
		}

		.footer {
			position: fixed;
			left: 0;
			bottom: 0;
			right: 0;
			z-index: 8;
			width: 100%;
			height: 122rpx;

			.img {
				width: 100%;
				height: 122rpx;
			}
		}

		/deep/ .u-size-mini {
			height: 70rpx;
			line-height: 70rpx;
		}

		/deep/ .uni-input-input,
		/deep/ .uni-input-placeholder {
			text-align: right !important;
		}

		padding: 0 34rpx 180rpx;

		.logo {
			display: block;
			width: 230rpx;
			margin:80rpx auto 10rpx;
		}

		.year {
			width: 527rpx;
			// height: 117rpx;
			display: block;
			margin: 50rpx auto 60rpx;
		}

		.sub {
			margin: 48rpx 0 0;
			height: 88rpx;
			font-family: AlibabaPuHuiTi, AlibabaPuHuiTi;
			font-weight: bold;
			font-size: 30rpx;
			color: #FFFFFF;
			line-height: 42rpx;
		}

		.from {
			background: #F7F8F3;
			border-radius: 27rpx;
			padding: 57rpx 31rpx 103rpx;

			.item {
				padding: 0 23rpx 0 49rpx;
				margin-bottom: 34rpx;
				height: 115rpx;
				background: #FFFFFF;
				border-radius: 25rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				&:last-child {
					margin-bottom: 0;
				}

				.name {
					font-family: Alibaba-PuHuiTi, Alibaba-PuHuiTi;
					font-weight: normal;
					font-size: 30rpx;
					color: #024546;
				}

				.info {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					flex: 1;
					text-align: right;

					image {
						width: 75rpx;
						height: 75rpx;
						background: #005850;
						border-radius: 50%;
					}

					.btn {
						margin-left: 23rpx;
					}
				}
			}
		}
	}
</style>
